
import jstoxml from 'jstoxml'
import FileSaver from 'file-saver';
import JSZip from 'jszip';
var xmlBody:any;
const zip = new JSZip();
const obj = {
  "kml": {
    "-xmlns": "http://www.opengis.net/kml/2.2",
    "-xmlns:wpml": "http://www.dji.com/wpmz/1.0.2",
    "Document": {
      "wpml:author": "Name",
      "wpml:createTime": "1637600807044",
      "wpml:updateTime": "1637600875837",
      "wpml:missionConfig": {
        "wpml:flyToWaylineMode": "safely",
        "wpml:finishAction": "goHome",
        "wpml:exitOnRCLost": "goContinue",
        "wpml:executeRCLostAction": "hover",
        "wpml:takeOffSecurityHeight": "20",
        "wpml:takeOffRefPoint": "23.98057,115.987663,100",
        "wpml:takeOffRefPointAGLHeight": "35",
        "wpml:globalTransitionalSpeed": "8",
        "wpml:droneInfo": {
          "wpml:droneEnumValue": "67",
          "wpml:droneSubEnumValue": "0"
        },
        "wpml:payloadInfo": {
          "wpml:payloadEnumValue": "52",
          "wpml:payloadSubEnumValue": "0",
          "wpml:payloadPositionIndex": "0"
        }
      },
      "Folder": {
        "wpml:templateType": "waypoint",
        "wpml:useGlobalTransitionalSpeed": "0",
        "wpml:templateId": "0",
        "wpml:waylineCoordinateSysParam": {
          "wpml:coordinateMode": "WGS84",
          "wpml:heightMode": "EGM96",
          "wpml:globalShootHeight": "50",
          "wpml:positioningType": "GPS",
          "wpml:surfaceFollowModeEnable": "1",
          "wpml:surfaceRelativeHeight": "100"
        },
        "wpml:autoFlightSpeed": "7",
        "wpml:gimbalPitchMode": "usePointSetting",
        "wpml:globalWaypointHeadingParam": {
          "wpml:waypointHeadingMode": "followWayline",
          "wpml:waypointHeadingAngle": "45",
          "wpml:waypointPoiPoint": "24.323345,116.324532,31.000000",
          "wpml:waypointHeadingPathMode": "clockwise"
        },
        "wpml:globalWaypointTurnMode": "toPointAndStopWithDiscontinuityCurvature",
        "wpml:globalUseStraightLine": "0",
        "Placemark": [
          {
            "Point": {
              "coordinates": "longitude,latitude"
            },
            "wpml:index": "0",
            "wpml:ellipsoidHeight": "90.2",//da
            "wpml:height": "100",//
            "wpml:useGlobalHeight": "1",
            "wpml:useGlobalSpeed": "1",
            "wpml:useGlobalHeadingParam": "1",
            "wpml:useGlobalTurnParam": "1",
            "wpml:gimbalPitchAngle": "0"
          },
          {
            "Point": {
              "coordinates": "longitude,latitude"
            },
            "wpml:index": "1",
            "wpml:ellipsoidHeight": "90.2",
            "wpml:height": "100",
            "wpml:useGlobalHeight": "1",
            "wpml:useGlobalSpeed": "1",
            "wpml:useGlobalHeadingParam": "1",
            "wpml:useGlobalTurnParam": "1",
            "wpml:gimbalPitchAngle": "0",
            "wpml:actionGroup": {
              "wpml:actionGroupId": "0",
              "wpml:actionGroupStartIndex": "1",
              "wpml:actionGroupEndIndex": "1",
              "wpml:actionGroupMode": "sequence",
              "wpml:actionTrigger": {
                "wpml:actionTriggerType": "reachPoint"
              },
              "wpml:action": [
                {
                  "wpml:actionId": "0",
                  "wpml:actionActuatorFunc": "gimbalRotate",
                  "wpml:actionActuatorFuncParam": {
                    "wpml:gimbalRotateMode": "absoluteAngle",
                    "wpml:gimbalPitchRotateEnable": "0",
                    "wpml:gimbalPitchRotateAngle": "0",
                    "wpml:gimbalRollRotateEnable": "0",
                    "wpml:gimbalRollRotateAngle": "0",
                    "wpml:gimbalYawRotateEnable": "1",
                    "wpml:gimbalYawRotateAngle": "30",
                    "wpml:gimbalRotateTimeEnable": "0",
                    "wpml:gimbalRotateTime": "0",
                    "wpml:payloadPositionIndex": "0"
                  }
                },
                {
                  "wpml:actionId": "1",
                  "wpml:actionActuatorFunc": "takePhoto",
                  "wpml:actionActuatorFuncParam": {
                    "wpml:fileSuffix": "point1",
                    "wpml:payloadPositionIndex": "0"
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

export function usePlineXml () {
  

  function downLoadXml(){
      const options = {
        compact: true
      };
      const xml = jstoxml.toXML(xmlBody, options);
      zip.file("template.kml", xml);
      zip.file("waylines.wpml", xml);
      
      zip.generateAsync({type:"blob"})
      .then(function(content) {
          // 下载Zip文件
          FileSaver.saveAs(content, "example.zip");
      });
  
  
      // var file = new File(xml, "hello world.xml", {type: "text/plain;charset=utf-8"});
  
  }



  
  // 获取大地水准面高
  // function theGetNormalHeight(longitude:number, latitude:number ) {
  //   // 使用 EGM2008 大地水准面模型

  // }
  function creatLineXml(from:any){
    const now = new Date();
    const timestamp = now.getTime();
    const timestampString = timestamp.toString();
    xmlBody = {
      "kml": {
        "-xmlns": "http://www.opengis.net/kml/2.2",
        "-xmlns:wpml": "http://www.dji.com/wpmz/1.0.2",
        "Document": {
          "wpml:author": "Name",
          "wpml:createTime": timestampString,
          "wpml:updateTime": timestampString,
          "wpml:missionConfig": {
            "wpml:flyToWaylineMode": "safely",
            "wpml:finishAction": from.finishAction,
            "wpml:exitOnRCLost": from.exitOnRCLost,
            "wpml:executeRCLostAction": from.executeRCLostAction,
            "wpml:takeOffSecurityHeight": from.takeOffSecurityHeight,
            "wpml:takeOffRefPoint": "23.98057,115.987663,100",
            "wpml:takeOffRefPointAGLHeight": "35",
            "wpml:globalTransitionalSpeed": from.globalTransitionalSpeed,
            "wpml:droneInfo": {
              "wpml:droneEnumValue": "67",
              "wpml:droneSubEnumValue": "0"
            },
            "wpml:payloadInfo": {
              "wpml:payloadEnumValue": "52",
              "wpml:payloadSubEnumValue": "0",
              "wpml:payloadPositionIndex": "0"
            }
          },
          "Folder": {
            "wpml:templateType": "waypoint",
            "wpml:useGlobalTransitionalSpeed": "0",
            "wpml:templateId": "0",
            "wpml:waylineCoordinateSysParam": {
              "wpml:coordinateMode": "WGS84",
              "wpml:heightMode": "EGM96",
              "wpml:globalShootHeight": "50",
              "wpml:positioningType": "GPS",
              "wpml:surfaceFollowModeEnable": "1",
              "wpml:surfaceRelativeHeight": "100"
            },
            "wpml:autoFlightSpeed": from.autoFlightSpeed,
            "wpml:gimbalPitchMode": "usePointSetting",
            "wpml:globalWaypointHeadingParam": {
              "wpml:waypointHeadingMode": "followWayline",
              "wpml:waypointHeadingAngle": "45",
              "wpml:waypointPoiPoint": "24.323345,116.324532,31.000000",
              "wpml:waypointHeadingPathMode": "clockwise"
            },
            "wpml:globalWaypointTurnMode": from.globalWaypointTurnMode,
            "wpml:globalUseStraightLine": "0",
            "Placemark": []
          }
        }
      }
    }

  }
  function addPointXml(point:any){
    const xmlPoint={
      "Point": {
        "coordinates": point.geometry.coordinates[0]+','+point.geometry.coordinates[1]
      },
      "wpml:index": point.id,
      // "wpml:ellipsoidHeight": "90.2",//
      "wpml:height": point.properties.h,//
      "wpml:useGlobalHeight": "0",
      "wpml:useGlobalSpeed": "1",
      "wpml:useGlobalHeadingParam": "1",
      "wpml:useGlobalTurnParam": "1",
      "wpml:gimbalPitchAngle": "0",//俯仰角
      "wpml:actionGroup":[]
    }
    return xmlPoint
    
  }
  function getTakePhotoAngle(point:any){
    const actionXml = {
      actionId: point.action.length,
      actionActuatorFunc: 'takePhoto',
      actionActuatorFuncParam: {
        fileSuffix: 'point1', //设置照片文件名的后缀。
        payloadPositionIndex: 0, //设置有效载荷的位置索引。0 表示主有效载荷（通常是相机）。
      },
    };
    return actionXml
  }
  function getActionAngle(point:any){
    const angleXml ={
        actionId: point.action.length,
        actionActuatorFunc: 'gimbalRotate',
        actionActuatorFuncParam: {
          gimbalRotateMode: 'absoluteAngle',
          gimbalPitchRotateEnable: 0,
          gimbalPitchRotateAngle: 0,
          gimbalRollRotateEnable: 0,
          gimbalRollRotateAngle: 0,
          gimbalYawRotateEnable: 1,
          gimbalYawRotateAngle: 30,
          gimbalRotateTimeEnable: 0,
          gimbalRotateTime: 0,
          payloadPositionIndex: 0,
        },
    }
    return angleXml
  }
  function finifshCreatFile(pointList:any){
    const xmlPoints:any[] = []
    let xmlPoint:any ;
    pointList.forEach((point,value) => {
      xmlPoint = addPointXml(point)
      xmlPoint["wpml:actionGroup"] = point.action
      xmlPoints.push(xmlPoint)
    });
    xmlBody.kml.Document.Folder.Placemark.push(xmlPoints)
    downLoadXml()
  }
  return {
    downLoadXml,
    creatLineXml,
    addPointXml,
    getActionAngle,
    getTakePhotoAngle,
    finifshCreatFile
  }

}

